#include "threadPool.h"
int initThreadPool(threadPool_t * pthreadPool, int workerNum){
    pthreadPool->workerNum = workerNum;
    pthreadPool->tidArr = (pthread_t *)calloc(pthreadPool->workerNum,sizeof(pthread_t));
    initQueue(&pthreadPool->taskQueue);
    return 0;
}
int main(int argc, char *argv[]){
    // ./server 192.168.118.128 1234 3
    ARGS_CHECK(argc,4);
    threadPool_t threadPool;
    int workerNum = atoi(argv[3]);
    initThreadPool(&threadPool,workerNum);
    // 初始化tcp
    int sockfd;
    tcpInit(argv[1],argv[2],&sockfd);
    // 创建子线程
    makeWorker(&threadPool);
    // 监听sockfd
    int epfd = epoll_create(1);
    epollAdd(epfd,sockfd); 
    struct epoll_event readyset[1024];
    while(1){
        int readynum = epoll_wait(epfd,readyset,1024,-1);
        for(int i = 0; i < readynum; ++i){
            if(readyset[i].data.fd == sockfd){
                int netfd = accept(sockfd,NULL,NULL);
                printf("1 client connected!\n");
                pthread_mutex_lock(&threadPool.taskQueue.mutex);
                enQueue(&threadPool.taskQueue,netfd);
                printf("1 netfd enqueue!\n");
                pthread_cond_signal(&threadPool.taskQueue.cond);
                pthread_mutex_unlock(&threadPool.taskQueue.mutex);
            }
        }
    }
}
